package src.ee.ioc.smaug.fsm;

import java.util.ArrayList;
import java.util.Random;

/**
 * Class for strong alphabet and working with it
 * @author Margarita Spichakova
 */
public class Alphabet {

    /**
     * Structure for storing alphabet
     */
    private ArrayList<String> alphabet;

    /**
     * Size of the alphabet
     */
    private int sizeOfAlphabet=0;

    /**
     *  Constructor. Defining alphabet and the size of it
     */
    public Alphabet() {
        this.alphabet = new ArrayList<String>();
        setSizeOfAlphabet();
    }

    /**
     * Add concrete symbol to the alphabet
     * @param symbol  add this symbol to alphabet
     */
    public void addToAlphabet(char symbol) {
        String s = String.valueOf(symbol);
        if (!isInAlphabet(symbol)) {
            alphabet.add(s);
            setSizeOfAlphabet();
        }
    }

    /**
     * Returns char at given position in alphabet
     * @param position of the symbol
     * @return symbol in given positioin
     */
    public char returnSymbolAtPosition(int position) {
        String symbol;
        char s;
        symbol = (String) alphabet.get(position);
        s = symbol.charAt(0);
        return s;
    }

    /**
     * Is given symbol in alphabet?
     * @param symbol given symbol we are searching for in alphabet
     * @return true if symbol is in alphabet, else - false
     */
    public boolean isInAlphabet(char symbol) {
        String sym;

        for (int i = 0; i < getSizeOfAlphabet(); i++) {
            sym = (String) alphabet.get(i);
            if (sym.compareTo(String.valueOf(symbol)) == 0)

                return true;

        }

        return false;
    }

    /**
     * Find position of the given symbol
     * @param s  given symbol 
     * @return position of the symbol
     */
    public int findPosition(char s) {
        int pos = 0;
        String sym;
        for (int i = 0; i < getSizeOfAlphabet(); i++) {
            sym = (String) alphabet.get(i);
            if (sym.compareTo(String.valueOf(s)) == 0) {
                pos = i;

            }
        }
        return pos;
    }
    
    /**
     * Generate random string in this alphabet
     * @param len length of required string
     * @return random string
     */
    public String generateRandomString (int len){
        String s="";
        Random rnd = new Random();
        for (int i=0; i<len;i++){
             int value = rnd.nextInt(this.getSizeOfAlphabet());
             s+=this.returnSymbolAtPosition(value);
        }
       
        return s;
    }
    
    /**
     * Size of the alphanet - getter
     * @return size of the alphabet
     */

    public int getSizeOfAlphabet() {
        return sizeOfAlphabet;
    }

    /**
     * Size of the alphabet setter
     */
    public void setSizeOfAlphabet() {
        this.sizeOfAlphabet = alphabet.size();
    }

    /**
     * Alphabet getter
     * @return alphabet
     */
    public ArrayList getAlphabet() {
        return alphabet;
    }

    /**
     * Alphabet setter
     * @param alphabet
     */
    public void setAlphabet(ArrayList<String> alphabet) {
        this.alphabet = alphabet;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
        String s="";
        for (int i=0;i<alphabet.size();i++) {
            s+=" "+alphabet.get(i);
        }
       
        return s;
    }
}
